using System.Collections.Generic;
using MEA.AE_Data.Config;

namespace MEA.AE_BusinessLogic
{
	public class IMA
	{
		private class SIMA
		{
			private sbyte ep;

			private sbyte id;

			private sbyte fl;

			private sbyte pi;

			private sbyte dqs;

			private sbyte pi2;

			private byte chk;

			private double qs = 0.1;

			private byte[] bytes = new byte[4];

			private double[] arrQSVal = new double[4] { 0.1, 0.15, 0.25, 0.5 };

			private uint bitSeq;

			private byte bitSeq7;

			public ConfigData.EIMACodeFrm iqaType = ConfigData.EIMACodeFrm.eIMACode6Lan;

			private IQAMap iqaMap = new IQAMap();

			public bool testChkSum(string inStr)
			{
				bool flag = true;
				bitSeq = iqaMap.iqaStr2UInt(inStr);
				if (inStr.Length == 7)
				{
					iqaType = ConfigData.EIMACodeFrm.eIMACode7Lan;
					bitSeq7 = (byte)iqaMap.findVal(inStr[6]);
					bitSeq7 <<= 3;
				}
				bitSeq2IQAVals(inStr);
				setArrayBytes();
				return testChkSum();
			}

			private bool testChkSum()
			{
				bool result = false;
				if (chk == calcChkSum())
				{
					result = true;
				}
				return result;
			}

			private byte calcChkSum()
			{
				byte b = 0;
				b = (byte)(ep + id + fl + pi + dqs + pi2);
				return (byte)(((b & 0xFF) + (b >> 4) + 1) & 0xF);
			}

			public void changeTo6Digit(ref string instr)
			{
				pi2 = 0;
				chk = calcChkSum();
				bitSeq7 = 0;
				bitSeq &= 4294967055u;
				bitSeq += (uint)((chk << 4) & 0xF0);
				setArrayBytes();
				instr = IQA2textStr();
			}

			public string IQA2textStr()
			{
				string text = string.Empty;
				uint num = bitSeq;
				num >>= 2;
				for (int i = 0; i < 6; i++)
				{
					int c = (int)(num & 0x1F);
					text = iqaMap.findChar(c) + text;
					num >>= 5;
				}
				return text;
			}

			private void bitSeq2IQAVals(string instr)
			{
				uint num = bitSeq;
				num >>= 2;
				dqs = (sbyte)(num & 3);
				if (dqs < 0 || dqs >= 4)
				{
					return;
				}
				num >>= 2;
				chk = (byte)(num & 0xF);
				num >>= 4;
				byte b = (byte)(num & 0x1F);
				if ((b & 0x10) == 16)
				{
					b |= 0xE0;
				}
				pi = (sbyte)b;
				num >>= 5;
				b = (byte)(num & 0x7F);
				if ((b & 0x40) == 64)
				{
					b |= 0xC0;
				}
				fl = (sbyte)b;
				num >>= 7;
				b = (byte)(num & 0x1F);
				if ((b & 0x10) == 16)
				{
					b |= 0xE0;
				}
				id = (sbyte)b;
				num >>= 5;
				b = (byte)(num & 0x7F);
				if ((b & 0x40) == 64)
				{
					b |= 0xC0;
				}
				ep = (sbyte)b;
				if (iqaType == ConfigData.EIMACodeFrm.eIMACode7Lan)
				{
					b = (byte)(bitSeq7 >> 3);
					if ((b & 0x10) == 16)
					{
						b |= 0xE0;
					}
					pi2 = (sbyte)b;
				}
			}

			private void setArrayBytes()
			{
				uint num = bitSeq;
				for (int num2 = 3; num2 >= 0; num2--)
				{
					bytes[num2] = (byte)(num & 0xFF);
					num >>= 8;
				}
			}

			public byte[] mkSendList()
			{
				return new byte[5]
				{
					bytes[0],
					bytes[1],
					bytes[2],
					bytes[3],
					0
				};
			}

			public string bytesToInt(byte[] inByte)
			{
				string empty = string.Empty;
				bitSeq = (uint)((inByte[0] << 24) | (inByte[1] << 16) | (inByte[2] << 8) | inByte[3]);
				if (bitSeq != 0)
				{
					return IQA2textStr();
				}
				return "not set";
			}
		}

		private string[] inText;

		private ConfigData.EIMACodeFrm imaFrmt;

		public ConfigData.EIMAErr[] imaErr;

		public List<byte[]> toSendList;

		public bool NoErr;

		private int strLan;

		private SIMA[] sIMAArr = new SIMA[4];

		private SIMA sIMA;

		public IMA(ConfigData.EIMACodeFrm infrm, ref string[] instr)
		{
			imaFrmt = infrm;
			string[] array = new string[4];
			inText = array;
			toSendList = new List<byte[]> { null, null, null, null };
			ConfigData.EIMAErr[] array2 = new ConfigData.EIMAErr[4];
			imaErr = array2;
			for (int i = 0; i < instr.Length; i++)
			{
				if (instr[i] == null)
				{
					continue;
				}
				switch (infrm)
				{
				case ConfigData.EIMACodeFrm.eIMACode7Lan:
					if (instr[i].Length == 7 || instr[i].Length == 6)
					{
						sIMA = new SIMA();
						if (sIMA.testChkSum(instr[i]))
						{
							sIMAArr[i] = sIMA;
							imaErr[i] = ConfigData.EIMAErr.eIMANoErr;
						}
						else
						{
							imaErr[i] = ConfigData.EIMAErr.eIMAChkSumErr;
						}
					}
					else
					{
						imaErr[i] = ConfigData.EIMAErr.eIMAInLanErr;
					}
					break;
				case ConfigData.EIMACodeFrm.eIMACode6Lan:
					if (instr[i].Length == 6)
					{
						sIMA = new SIMA();
						if (sIMA.testChkSum(instr[i]))
						{
							sIMAArr[i] = sIMA;
							imaErr[i] = ConfigData.EIMAErr.eIMANoErr;
						}
						else
						{
							imaErr[i] = ConfigData.EIMAErr.eIMAChkSumErr;
						}
					}
					else
					{
						imaErr[i] = ConfigData.EIMAErr.eIMAInLanErr;
					}
					break;
				}
			}
			for (int j = 0; j < sIMAArr.Length; j++)
			{
				sIMA = sIMAArr[j];
				if (sIMA == null)
				{
					continue;
				}
				if (infrm == ConfigData.EIMACodeFrm.eIMACode7Lan)
				{
					sIMA = sIMAArr[j];
					if (sIMA.iqaType == ConfigData.EIMACodeFrm.eIMACode7Lan)
					{
						sIMA.changeTo6Digit(ref instr[j]);
						imaErr[j] = ConfigData.EIMAErr.eIMAChngdTo6Digit;
					}
				}
				toSendList[j] = sIMA.mkSendList();
			}
		}

		public IMA()
		{
			sIMA = new SIMA();
		}

		public string vonBytesToString(byte[] inByte)
		{
			return sIMA.bytesToInt(inByte);
		}
	}
}
